<p>
  Pandas is one of the most powerful tools for dealing with financial data. First we need to import Pandas:
</p>

<div class="section-example-container">

<pre class="python">import pandas as pd</pre>
</div>

<h4>Series</h4>

<p>
  Series is a one-dimensional labeled array capable of holding any data type (integers, strings, float, Python object, etc.)
</p>

<p>
  We create a Series by calling pd.Series(data), where data can be a dictionary, an array or just a scalar value.
</p>

<div class="section-example-container">

<pre class="python">price = [143.73, 145.83, 143.68, 144.02, 143.5, 142.62]
s = pd.Series(price)
print s

0    143.73
1    145.83
2    143.68
3    144.02
4    143.50
5    142.62</pre>
</div>

<p>
  We can customize the indices of a new Series:
</p>

<div class="section-example-container">

<pre class="python">s = pd.Series(price, index = ['a', 'b', 'c', 'd', 'e', 'f'])
print s

a    143.73
b    145.83
c    143.68
d    144.02
e    143.50
f    142.62</pre>
</div>

<p>
  Or we can change the indices of an existing Series:
</p>
<div class="section-example-container">

<pre class="python">s.index = [6,5,4,3,2,1]
print s

6    143.73
5    145.83
4    143.68
3    144.02
2    143.50
1    142.62</pre>
</div>

<p>
  Series is like a list since it can be sliced by index:
</p>
<div class="section-example-container">

<pre class="python">print s[1:]
print s[:-2]

5    145.83
4    143.68
3    144.02
2    143.50
1    142.62
dtype: float64
6    143.73
5    145.83
4    143.68
3    144.02
dtype: float64</pre>
</div>

<p>
  Series is also like a dictionary whose values can be set or fetched by index label:
</p>

<div class="section-example-container">

<pre class="python">print s[4]
s[4] = 0
print s

143.68
6    143.73
5    145.83
4      0.00
3    144.02
2    143.50
1    142.62
dtype: float64</pre>
</div>

<p>
  Series can also have a name attribute, which will be used when we make up a Pandas DataFrame using several series.
</p>

<div class="section-example-container">

<pre class="python">s = pd.Series(price, name = 'Apple Prices')
print s
print s.name

0    143.73
1    145.83
2    143.68
3    144.02
4    143.50
5    142.62
Name: Apple Prices, dtype: float64
Apple Prices</pre>
</div>

<p>
  We can get the statistical summaries of a Series:
</p>

<div class="section-example-container">

<pre class="python">print s.describe()

count      6.000000
mean     143.896667
std        1.059711
min      142.620000
25%      143.545000
50%      143.705000
75%      143.947500
max      145.830000</pre>
</div>

<h4>Time Index</h4>

<p>
  Pandas has a built-in function specifically for creating date indices: pd.date_range(). We use it to create a new index for our Series:
</p>

<div class="section-example-container">

<pre class="python">time_index = pd.date_range('2017-01-01', periods = len(s), freq = 'D')
print time_index
s.index = time_index
print s

DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06'],
              dtype='datetime64[ns]', freq='D')
2017-01-01    143.73
2017-01-02    145.83
2017-01-03    143.68
2017-01-04    144.02
2017-01-05    143.50
2017-01-06    142.62
Freq: D, Name: Apple Prices, dtype: float64</pre>
</div>

<p>
  Series are usually accessed using the iloc[] and loc[] methods. iloc[] is used to access elements by integer index, and loc[] is used to access the index of the series.
</p>

<p>
  iloc[] is necessary when the index of a series are integers, take our previous defined series as example:
</p>

<div class="section-example-container">

<pre class="python">s.index = [6,5,4,3,2,1]
print s
print s[1]

6    143.73
5    145.83
4    143.68
3    144.02
2    143.50
1    142.62
Name: Apple Prices, dtype: float64
142.62</pre>
</div>

<p>
  If we intended to take the second element of the series, we would make a mistake here, because the index are integers. In order to access to the element we want, we use iloc[] here:
</p>

<div class="section-example-container">

<pre class="python">print s.iloc[1]
[out]: 145.83
</pre>
</div>

<p>
  While working with time series data, we often use time as the index. Pandas provides us with various methods to access the data by time index.
</p>

<div class="section-example-container">

<pre class="python">s.index = time_index
print s['2017-01-03']
[out]: 143.68</pre>
</div>

<p>
  We can even access to a range of dates:
</p>

<div class="section-example-container">

<pre class="python">print s['2017-01-02':'2017-01-05']

2017-01-02    145.83
2017-01-03    143.68
2017-01-04    144.02
2017-01-05    143.50
Freq: D, Name: Apple Prices, dtype: float64</pre>
</div>

<p>
  Series[] provides us a very flexible way to index data. We can add any condition in the square brackets:
</p>

<div class="section-example-container">

<pre class="python">print s[s &lt; np.mean(s)]
print s[(s &gt; np.mean(s)) &amp; (s &lt; np.mean(s) + 1.64*np.std(s))]

2017-01-01    143.73
2017-01-03    143.68
2017-01-05    143.50
2017-01-06    142.62
Name: Apple Prices, dtype: float64
2017-01-04    144.02
Freq: D, Name: Apple Price List, dtype: float64</pre>
</div>

<p>
  As demonstrated, we can use logical operators like &amp; (and), | (or) and ~ (not) to group multiple conditions.
</p>
